package org.example.practice.controller;

import org.example.practice.entity.MessageInfo;
import org.example.practice.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;
    private List<MessageInfo> messageList=new ArrayList<>();
    @PostMapping("/publish")
    public Boolean publish(@RequestBody MessageInfo messageInfo){
        System.out.println("接收到参数, messaage:" + messageInfo);
        if (!StringUtils.hasLength(messageInfo.getMessage())
                   || !StringUtils.hasLength(messageInfo.getFrom())
                    || !StringUtils.hasLength(messageInfo.getTo()))   {
            return false;
        }
        Integer result=messageService.insertMessage(messageInfo);
        return result==1;
    }
    @GetMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.selectAll();
    }
}
